package com.sunmouse.toolkit.fragment.custom;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.sunmouse.toollib.utils.DialogUtil;
import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toollib.utils.TipsUtils;
import com.sunmouse.toollib.widget.LoadingDialog;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.fragment.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class DialogFragment extends BaseFragment {


    @BindView(R.id.start_btn)
    Button startBtn;
    @BindView(R.id.stop_btn)
    Button stopBtn;
    @BindView(R.id.dialog_btn)
    Button dialogBtn;
    Unbinder unbinder;
    @BindView(R.id.demo_btn)
    Button demoBtn;

    private LoadingDialog mDialog;
    private TipsUtils mTips;

    private DialogUtil mUtil;
    private EditText ipEt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_dialog, container, false);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }


    @Override
    public void init() {
        startBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);
        dialogBtn.setOnClickListener(this);
        demoBtn.setOnClickListener(this);


        mDialog = LoadingDialog.getLoadingDialog(getActivity());
        mDialog.setDialogScale(200);
        mDialog.setLoadingText("");

        mTips = TipsUtils.getInstance(getActivity());
        mTips.setOnCancelClickListener(null);
        mTips.setOnConfirmClickListener(null);
        mTips.setAnim(R.style.Anim_TopInTopOut);
        mTips.setCancelBackground(R.drawable.dialog_selector);
        mTips.setConfirmBackground(R.drawable.dialog_selector);

        View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_setting_ip, null);
        ipEt = (EditText) v.findViewById(R.id.ip_et);
        this.ipEt.setOnClickListener(this);
        DisplayMetrics metrics=getResources().getDisplayMetrics();
        this.mUtil = DialogUtil.init()
                .view(v)
                .width((int) (metrics.widthPixels*0.8))
                .height((int) (metrics.heightPixels*0.4))
                .themeId(DialogUtil.THEME_NORMAL)
                .mode(DialogUtil.Builder.NORMAL)
                .build(getActivity());
    }

    @Override
    public void onClick(View v) {
        if (v==ipEt){
//            mUtil.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//            SystemUtils.showKeyboard(ipEt);
            return;
        }
        if (v == demoBtn) {
//            mUtil.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            mUtil.show();
            return;
        }
        switch (v.getId()) {
            case R.id.start_btn:
                mDialog.show();
                LogUtils.d("------start_btn");
                break;
            case R.id.stop_btn:
                LoadingDialog.getLoadingDialog(getActivity()).dismiss();
                LogUtils.d("------stop_btn");
                break;
            case R.id.dialog_btn:
                mTips.show();
                LogUtils.d("------dialog_btn");
                break;
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
